﻿using Ease.EaseMission.Runtime;
using Ease.EaseNode.Editor.NodeView;
using Ease.EaseNode.Runtime;
using Ease.EaseNode.Runtime.Param;
using UnityEngine;

namespace Ease.EaseMission.Editor.Node
{
    public abstract class BaseMissionNode<T> : BaseEaseNode where T : class, IBaseMissionData
    {
        protected T missionData;

        public BaseMissionNode(IEaseNodeDataState state) : base(state)
        {
            title = State == null ? $"{typeof(T).Name}" : $"{typeof(T).Name}:{state.Key}";
            contentContainer.style.backgroundColor = Color.gray;
            missionData = State?.Data as T;
            title = missionData?.Tip;
            SetNodeByState(State.CurrentNodeState);
        }

        protected override void OnStateChange(BaseChangeParam param)
        {
            if (param is DataChangeParam a)
            {
                OnRefreshAll();
            }
            else if (param is StateChangeParam b)
            {
                SetNodeByState(b.newState);
            }
        }

        protected virtual void SetNodeByState(NodeState state)
        {
            contentContainer.style.backgroundColor = NodeVisual.GetStateColor(state);
        }

        protected virtual void OnRefreshAll()
        {
            title = missionData.Tip;
        }
    }
}